import { createRouter, createWebHashHistory } from "vue-router";
import store from "../store";
import { getUserInfo, logtime } from "../api/user";

// 1.创建路由规则
const routes = [
  {
    path: '/login',
    name: "login",
    component: () => import("../views/login/index.vue")
  },
  {
    path: '/reg',
    name: "reg",
    component: () => import("../views/reg/index.vue")
  },
  {
    path: '/editor',
    name: "editor",
    component: () => import("../views/editor/index.vue"),
    props: true,
  },
  {
    path: '/notification/:notificationId',
    name: "notification",
    component: () => import("../views/notification/index.vue"),
    props: true,
  },
  {
    path: '/event/:eventId',
    name: "event",
    component: () => import("../views/event/index.vue"),
    props: true,
  },
  {
    path: '/club/:clubId',
    name: "club",
    component: () => import("../views/clubinfo/index.vue"),
    props: true,
  },
  {
    path: '/apply/:clubId',
    name: "apply",
    component: () => import("../views/apply/index.vue"),
    props: true,
  },
  {
    path: "/myvote/:voteId",
    name: "vote",
    component: () => import("../views/voteinfo/index.vue"),
    props:true,
  },
  {
    path: '/',
    name: 'layout',
    component: () => import("../views/layout/index.vue"),
    children: [
      {
        path: "",
        name: "home",
        component: () => import("../views/home/index.vue"),
        meta: { requiresAuth: true,
       
        } // 添加元字段标记该路由需要登录权限
      },
      {
        path: "/clubs",
        name: "clubs",
        component: () => import("../views/clubs/index.vue"),
        meta: { requiresAuth: true } // 添加元字段标记该路由需要登录权限
      },
      {
        path: "/dt",
        name: "dt",
        component: () => import("../views/dt/index.vue"),
        meta: { requiresAuth: true } // 添加元字段标记该路由需要登录权限
      },

      {
        path: "/my",
        name: "my",
        component: () => import("../views/my/index.vue"),
        meta: { requiresAuth: true } // 添加元字段标记该路由需要登录权限
      },
      {
        path: '/getapply/:applyId',
        name: "getapply",
        component: () => import("../views/applyInfo/index.vue"),
        props: true,
      },
      {
        path: "/myinfo",
        name: "myinfo",
        component: () => import("../views/myinfo/index.vue"),
        meta: { requiresAuth: true } // 添加元字段标记该路由需要登录权限
      },
      {
        path: "/myclub",
        name: "myclub",
        component: () => import("../views/myclub/index.vue"),
        meta: { requiresAuth: true } // 添加元字段标记该路由需要登录权限
      },
      {
        path: "/mynotification",
        name: "mynotification",
        component: () => import("../views/mynotification/index.vue"),
        meta: { requiresAuth: true } // 添加元字段标记该路由需要登录权限
      },
      {
        path: "/myevent",
        name: "myevent",
        component: () => import("../views/myevent/index.vue"),
        meta: { requiresAuth: true } // 添加元字段标记该路由需要登录权限
      },
      {
        path: "/myvote",
        name: "myvote",
        component: () => import("../views/vote/index.vue"),
        meta: { requiresAuth: true } // 添加元字段标记该路由需要登录权限
      },
      {
        path: "/mypart",
        name: "mypart",
        component: () => import("../views/mypart/index.vue"),
        meta: { requiresAuth: true } // 添加元字段标记该路由需要登录权限
      },
      {
        path: "/myapply",
        name: "myapply",
        component: () => import("../views/myapply/index.vue"),
        meta: { requiresAuth: true } // 添加元字段标记该路由需要登录权限
      },
      
      

    ]
  }
]

// 2.创建路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 添加全局前置守卫，用于检查每个需要登录权限的路由
router.beforeEach(async (to, from, next) => {
  // 判断该路由是否需要登录权限
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    // 判断用户是否已经登录并且 token 没有过期
// debugger
console.log(store.state.user);
    const user = store.state.user;
    if (user && user.data.myToken) {
      const currentTime = Math.floor(new Date().getTime() / 1000);
      if (user.data.exp > currentTime && user.data.exp - currentTime > 300) {
        
        // token 未过期，可以通过验证
        next();
      } else {
        // token 已过期，需要重新获取用户信息
        try {
            
        const {data}=await logtime();
        if(data.data!='error'){
            const newToken=data.data;
            const newUser = Object.assign({},
                 store.state.user={
                data: {
                  ...store.state.user.data,
                  myToken: newToken
                }
              });
    
              store.commit("setUser", newUser);
        }else{
            alert("长时间没有操作，登录失效，请你重新登录");
            store.commit('setUser',null);
             next({
                path: "/login",
                query: { redirect: to.fullPath },
              });
        }
     
        next();
        } catch (error) {
          // 获取用户信息失败，跳转到登录页面
          next({
            path: "/login",
            query: { redirect: to.fullPath },
          });
        }
      }
    } else {
      // 用户未登录，跳转到登录页面
      next({
        path: "/login",
        query: { redirect: to.fullPath },
      });
    }
  } else {
    // 不需要登录权限的路由，直接通过验证
    next();
  }
});

export default router;
